/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * Class for clouds moving on the background.
 */

#include "GameClouds.h"
#include "../../Resources.h"
#include "../../Common.h"
#include "../layers/LayerBackground.h"

#include <vector>
#include <cstdlib>

using namespace cocos2d;

// constructor
GameClouds::GameClouds( LayerBackground *layerBackground ) : mDelay( 0 ), mLayerBackground( layerBackground ) {
	// randomize few first clouds to the sky
	for( unsigned int i = 0; i < 5; ++i ) {
		// create sprite
		CloudProperties *newCloud = new CloudProperties();
		switch( rand() % 3 ) {
		case 0: newCloud->CloudSprite = cocos2d::CCSprite::spriteWithFile( getResource( Textures::GAME_MISC_CLOUD1 ) );
			break;
		case 1: newCloud->CloudSprite = cocos2d::CCSprite::spriteWithFile( getResource( Textures::GAME_MISC_CLOUD2 ) );
			break;
		case 2: newCloud->CloudSprite = cocos2d::CCSprite::spriteWithFile( getResource( Textures::GAME_MISC_CLOUD3 ) );
			break;
		}
		newCloud->CloudSprite->setPosition( ccp( - WIN_WIDTH * 0.5f + ( rand() % 100 ) / 100.0f * WIN_WIDTH * 2.0f, WIN_HEIGHT * 0.9f + ( rand() % 100 ) / 100.0f * WIN_HEIGHT * 0.3f ) );
		newCloud->CloudSprite->setScale( RATEX * 2.0f );
		newCloud->CloudSprite->setOpacity( 50.0f );
		mLayerBackground->addChild( newCloud->CloudSprite, Z_LAYER_GAME_EFFECTS );
		// run move action
		newCloud->CloudAction = cocos2d::CCMoveBy::actionWithDuration( 40.0f + rand() % 100 / 100 * 20.0f, ccp( WIN_WIDTH * 2.5f, 0 ) );
		newCloud->CloudAction->retain();
		newCloud->CloudSprite->runAction( newCloud->CloudAction );
		mClouds.push_back( newCloud );

		// opacity & scale actions
		CCFiniteTimeAction *a1 = CCFadeTo::actionWithDuration( 15.0f, 50 );
		CCFiniteTimeAction *a2 = CCFadeTo::actionWithDuration( 25.0f, 150 );
		CCSequence *s1 = CCSequence::actionOneTwo( a1, a2 );
		newCloud->CloudSprite->runAction( CCRepeatForever::actionWithAction( s1 ) );
		CCFiniteTimeAction *a3 = CCScaleTo::actionWithDuration( 15.0f, RATEX * 1.5f );
		CCFiniteTimeAction *a4 = CCScaleTo::actionWithDuration( 20.0f, RATEX * 2.5f );
		CCSequence *s2 = CCSequence::actionOneTwo( a3, a4 );
		newCloud->CloudSprite->runAction( CCRepeatForever::actionWithAction( s2 ) );
	}
}

// destructor
GameClouds::~GameClouds() {
	for( unsigned int i = 0; i < mClouds.size(); ++i ) {
		mLayerBackground->removeChild( mClouds.at( i )->CloudSprite, true );
		delete mClouds.at( i );
	}
}

// default update function
void GameClouds::update( cocos2d::ccTime dt ) {
	mDelay -= dt;

	if( mDelay <= 0.0f ) {
		// create sprite
		CloudProperties *newCloud = new CloudProperties();
		switch( rand() % 3 ) {
		case 0: newCloud->CloudSprite = cocos2d::CCSprite::spriteWithFile( getResource( Textures::GAME_MISC_CLOUD1 ) );
			break;
		case 1: newCloud->CloudSprite = cocos2d::CCSprite::spriteWithFile( getResource( Textures::GAME_MISC_CLOUD2 ) );
			break;
		case 2: newCloud->CloudSprite = cocos2d::CCSprite::spriteWithFile( getResource( Textures::GAME_MISC_CLOUD3 ) );
			break;
		}
		newCloud->CloudSprite->setPosition( ccp( -WIN_WIDTH * 1.2f, WIN_HEIGHT * 0.8f + rand() % 100 / 100.0f * WIN_HEIGHT * 0.3f ) );
		newCloud->CloudSprite->setScale( RATEX * 1.5f );
		mLayerBackground->addChild( newCloud->CloudSprite, Z_LAYER_GAME_EFFECTS );
		// run move action
		newCloud->CloudAction = cocos2d::CCMoveTo::actionWithDuration( 60.0f + rand() % 100 / 100 * 20.0f, ccp( WIN_WIDTH * 2.5f, newCloud->CloudSprite->getPositionY() ) );
		newCloud->CloudAction->retain();
		newCloud->CloudSprite->runAction( newCloud->CloudAction );
		mClouds.push_back( newCloud );

		// opacity & scale actions
		CCFiniteTimeAction *a1 = CCFadeTo::actionWithDuration( 15.0f, 50 );
		CCFiniteTimeAction *a2 = CCFadeTo::actionWithDuration( 25.0f, 150 );
		CCSequence *s1 = CCSequence::actionOneTwo( a1, a2 );
		newCloud->CloudSprite->runAction( CCRepeatForever::actionWithAction( s1 ) );
		
		CCFiniteTimeAction *a3 = CCScaleTo::actionWithDuration( 15.0f, RATEX * 1.5f );
		CCFiniteTimeAction *a4 = CCScaleTo::actionWithDuration( 20.0f, RATEX * 2.5f );
		CCSequence *s2 = CCSequence::actionOneTwo( a3, a4 );
		newCloud->CloudSprite->runAction( CCRepeatForever::actionWithAction( s2 ) );

		// randomize time for the next cloud
		mDelay = 3.0f + rand() % 100 * 100.0f * 3.0f;
	}

	for( unsigned int i = 0; i < mClouds.size(); ++i ) {
		if( mClouds.at( i )->CloudAction->isDone() ) {
			mLayerBackground->removeChild( mClouds.at( i )->CloudSprite, true );
			delete mClouds.at( i );
			mClouds.erase( mClouds.begin() + i );
			--i;
		}
	}

}
